package org.tarena.dang.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.tarena.dang.dao.UserDAO;
import org.tarena.dang.entity.User;
import org.tarena.dang.util.DbUtil;
import org.tarena.dang.util.EncryptUtil;


public class UserDAOImpl implements UserDAO {
	private static final String SAVE = 
			"insert into d_user(email,nickname,password," +
			"user_integral,is_email_verify,email_verify_code," +
			"last_login_time,last_login_ip) " +
			"values (?,?,?,?,?,?,?,?)";
	private static final String UPDATE = "update d_user set email_verify_code=?,last_login_time=? where id=? ";
	public void save(User user) throws SQLException {
		PreparedStatement pst = 
				DbUtil.openConnection()
					.prepareStatement(SAVE,
					Statement.RETURN_GENERATED_KEYS);
		pst.setString(1, user.getEmail());
		pst.setString(2, user.getNickname());
		pst.setString(3, user.getPassword());
		pst.setInt(4, user.getUserIntegral());
		String emailVerify = 
					user.isEmailVerify()?"Y":"N";
		pst.setString(5, emailVerify);
		pst.setString(6, user.getEmailVerifyCode());
		pst.setLong(7, user.getLastLoginTime());
		pst.setString(8, user.getLastLoginIp());
		pst.executeUpdate();
		ResultSet rs = pst.getGeneratedKeys();
		rs.next();
		int id = rs.getInt(1);
		user.setId(id);
		DbUtil.closeConnection();
	}

	@Override
	public void update(User user) throws SQLException {
		PreparedStatement pst = 
			DbUtil.openConnection()
				.prepareStatement(UPDATE);
		pst.setString(1, user.getEmailVerifyCode());
		pst.setLong(2, user.getLastLoginTime());
		pst.setInt(3, user.getId());
		pst.executeUpdate();
		DbUtil.closeConnection();
		
	}

	@Override
	public String findByEmailCode(int id) throws SQLException  {
		String sql = "select email_verify_code from d_user where id="+id;
		PreparedStatement pst = DbUtil.openConnection().prepareStatement(sql);
		
		ResultSet rs = pst.executeQuery();
		//System.out.println(rs.first());
		rs.next();
		String code = EncryptUtil.Base64Encrypt((rs.getString(1)+"-"+id).getBytes());
		DbUtil.closeConnection();
		System.out.println(code);
		return code;
	}
	
	@Override
	public User findById(int id) throws SQLException {
		User user = new User();
		String sql = "select * from d_user where id="+id;
		PreparedStatement pst = DbUtil.openConnection().prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		while(rs.next()){
			user.setEmail(rs.getString("email"));
			user.setEmailVerify(rs.getBoolean("is_email_verify"));
			user.setEmailVerifyCode(rs.getString("email_verify_code"));
			user.setId(rs.getInt("id"));
			user.setNickname(rs.getString("nickname"));
		}
		
		DbUtil.closeConnection();
		return user;
	}

	//登录验证
	@Override
	public User findByPwd(String email, String password)  {
		User user = new User();
		//user = null;
		
		String sql = "select * from d_user where email=? and password=?";
		System.out.println(email+"(((((((((((((("+password);
		PreparedStatement pst;
		try {
			pst = DbUtil.openConnection().prepareStatement(sql);
			pst.setString(1, email);
			pst.setString(2, password);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				System.out.println("sss");
				user.setEmail(rs.getString("email"));
				user.setEmailVerify(rs.getString("is_email_verify").equals("N")?false:true);
				user.setEmailVerifyCode(rs.getString("email_verify_code"));
				user.setLastLoginTime(rs.getLong("last_login_time"));
				user.setId(rs.getInt("id"));
				user.setNickname(rs.getString("nickname"));
			}
			System.out.println(user.isEmailVerify());
			System.out.println("用户邮箱是否验证："+user.isEmailVerify());
			return user;
			
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("验证码密码有误！");
			return null;
		}finally{
			try {
				DbUtil.closeConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	

	@Override
	public void updateEmailVerify(int id) throws SQLException {
		String sql = "update d_user set is_email_verify=? where id="+id;
		PreparedStatement pst = DbUtil.openConnection().prepareStatement(sql);
		pst.setString(1,"Y");
		pst.executeUpdate();
		DbUtil.closeConnection();
	}

	@Override
	public boolean findByEmail(String email)  {
		String sql = "select id from d_user where email=?";
		PreparedStatement pst;
		
			try {
				pst = DbUtil.openConnection().prepareStatement(sql);
				pst.setString(1,email);
				ResultSet rs = pst.executeQuery();
				return rs.next();
			} catch (SQLException e) {
				System.out.println("验证邮箱出错了");
				e.printStackTrace();
				return true;
			}finally{
				try {
					DbUtil.closeConnection();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
	}
	
	public static void main(String[] args) throws SQLException {
		UserDAOImpl daoImpl = new UserDAOImpl();
		daoImpl.findByPwd("zhubo719@12.co","");
		//daoImpl.findByEmail("zhubo719@12.co");
	}
}
